<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.studentgrades.dao.ScoreDAO" %>
<%@ page import="com.example.studentgrades.model.Score" %>
<%@ page import="com.example.studentgrades.model.Student" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.List" %>
<%
  Student student = (Student) session.getAttribute("student");
  if (student == null) {
    response.sendRedirect("login.jsp");
    return;
  }

  List<Score> scores = null;
  try (Connection connection = com.example.studentgrades.util.ConnectionManager.getConnection()) {
    ScoreDAO scoreDAO = new ScoreDAO(connection);
    scores = scoreDAO.findScoreById(student.getStudentId());
  } catch (Exception e) {
    e.printStackTrace();
  }
%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>成绩展示</title>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
  <h2 class="text-center"> <%= student.getUsername() %>的成绩单</h2>
  <table class="table table-bordered mt-4">
    <thead class="thead-dark">
    <tr>
      <th>课程</th>
      <th>成绩</th>
    </tr>
    </thead>
    <tbody>
    <% if (scores != null) {
      for (Score score : scores) { %>
    <tr>
      <td><%= score.getSubject() %></td>
      <td><%= score.getScore() %></td>
    </tr>
    <% } } %>
    </tbody>
  </table>
  <a href="student_home.jsp" class="btn btn-primary">返回</a>
</div>
</body>
</html>
